﻿using Microsoft.SharePoint.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SPSearchAsYouTypeWP.SearchBoxAsYouType
{
    public class HttpEncodingUtility : SPHttpUtility
    {
        private const string c_strHref = "href";

        private const string c_strSrc = "src";

        private const string c_strBackground = "background";

        private HttpEncodingUtility()
        {
        }

        public static string HtmlAttributeDecode(string attributeToDecode)
        {
            return SPHttpUtility.HtmlDecode(attributeToDecode);
        }

        public static string HtmlAttributeEncode(string attributeToEncode)
        {
            return SPHttpUtility.HtmlEncode(attributeToEncode);
        }

        public static void HtmlAttributeEncode(string attributeToEncode, TextWriter output)
        {
            SPHttpUtility.HtmlEncode(attributeToEncode, output);
        }

        public static void HtmlAttributeEncode(string attributeName, string attributeToEncode, TextWriter output)
        {
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }
            output.Write(' ');
            output.Write(attributeName);
            output.Write("=\"");
            SPHttpUtility.HtmlEncode(attributeToEncode, output);
            output.Write('\"');
        }

        public static void HtmlUrlAttributeEncode(string attributeName, string urlAttributeToEncode, TextWriter output)
        {
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }
            output.Write(' ');
            output.Write(attributeName);
            output.Write("=\"");
            SPHttpUtility.HtmlUrlAttributeEncode(urlAttributeToEncode, output);
            output.Write('\"');
        }

        private static bool IsAllowedAttributeName(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return false;
            }
            return !Regex.IsMatch(value, "[^a-zA-Z_0-9.:-]+", RegexOptions.Compiled);
        }

        private static bool IsURLTypeAttributeName(string strAttributeName)
        {
            string str = strAttributeName.Trim();
            if ("href".Equals(str, StringComparison.OrdinalIgnoreCase) || "src".Equals(str, StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
            return "background".Equals(str, StringComparison.OrdinalIgnoreCase);
        }

        public static string UrlPathDecode(string urlToDecode)
        {
            return SPHttpUtility.UrlPathDecode(urlToDecode, false);
        }

        public static string UrlPathEncode(string urlToEncode)
        {
            return SPHttpUtility.UrlPathEncode(urlToEncode, false);
        }

        public static void UrlPathEncode(string urlToEncode, TextWriter output)
        {
            SPHttpUtility.UrlPathEncode(urlToEncode, false, output);
        }
    }
}
